package com.mojang.ld22.level.tile;

import java.util.Random;

import javax.swing.JOptionPane;

import com.mojang.ld22.entity.Entity;
import com.mojang.ld22.entity.Mob;
import com.mojang.ld22.entity.Player;
import com.mojang.ld22.gfx.Screen;
import com.mojang.ld22.item.Item;
import com.mojang.ld22.item.resource.Resource;
import com.mojang.ld22.level.Level;

public class Tile {
	public static int tickCount = 0;
	protected Random random = new Random();

	public static Tile[] tiles = new Tile[256];
	public static Tile grass = new GrassTile(0, 0x208020);
	public static Tile rock = new RockTile(1, 0xa0a0a0);
	public static Tile water = new WaterTile(2, 0x000080);
	public static Tile flower = new FlowerTile(3, 0xffff00);
	public static Tile tree = new TreeTile(4, 0x003000);
	public static Tile dirt = new DirtTile(5, 0x604040);
	public static Tile sand = new SandTile(6, 0xa0a040);
	public static Tile cactus = new CactusTile(7, 0x003300);
	public static Tile hole = new HoleTile(8, 0xffffff);
	public static Tile treeSapling = new SaplingTile(9, grass, tree, 0x82c46b);
	public static Tile cactusSapling = new SaplingTile(10, sand, cactus, 0x98d08b);
	public static Tile farmland = new FarmTile(11, 0x604060);
	public static Tile wheat = new WheatTile(12, 0xffb800);
	public static Tile lava = new LavaTile(13, 0xff2020);
	public static Tile stairsDown = new StairsTile(14, false, 0x0000ff);
	public static Tile stairsUp = new StairsTile(15, true, 0xff0000);
	public static Tile infiniteFall = new InfiniteFallTile(16, 0xffff80);
	public static Tile cloud = new CloudTile(17, 0xa1a1a1);
	public static Tile hardRock = new HardRockTile(18, 0x000000);
	public static Tile ironOre = new OreTile(19, Resource.ironOre, 0xdcdcdc);
	public static Tile goldOre = new OreTile(20, Resource.goldOre, 0xd3cb10);
	public static Tile gemOre = new OreTile(21, Resource.gem, 0x80d0d0);
	public static Tile cloudCactus = new CloudCactusTile(22, 0xff00ff);

	public final byte id;
	public final int hexValue;

	public boolean connectsToGrass = false;
	public boolean connectsToSand = false;
	public boolean connectsToLava = false;
	public boolean connectsToWater = false;

	public Tile(int id, int hexValue) {
		this.id = (byte) id;
		this.hexValue = (int) hexValue;
		
		if (tiles[id] != null) throw new RuntimeException("Duplicate tile ids!");
		tiles[id] = this;
	}
	
	public static int getHexVal(int id)
	{
		if(tiles[id] != null)
			return tiles[id].hexValue;
		else
			return -1;
	}
	
	public static int getId(int hexVal)
	{
		for(int i = 0; i < tiles.length; i++)
		{
			if(tiles[i] == null)
				return -1;

			if(tiles[i].hexValue == hexVal)
				return i;
		}
		return -1;
	}
	
	public void render(Screen screen, Level level, int x, int y) {
	}

	public boolean mayPass(Level level, int x, int y, Entity e) {
		return true;
	}

	public int getLightRadius(Level level, int x, int y) {
		return 0;
	}

	public void hurt(Level level, int x, int y, Mob source, int dmg, int attackDir) {
	}

	public void bumpedInto(Level level, int xt, int yt, Entity entity) {
	}

	public void tick(Level level, int xt, int yt) {
	}

	public void steppedOn(Level level, int xt, int yt, Entity entity) {
	}

	public boolean interact(Level level, int xt, int yt, Player player, Item item, int attackDir) {
		return false;
	}

	public boolean use(Level level, int xt, int yt, Player player, int attackDir) {
		return false;
	}

	public boolean connectsToLiquid() {
		return connectsToWater || connectsToLava;
	}
	
	public static void main(String[] args) {
		boolean bNoProblem = true;
		for(int i = 0; i < tiles.length; i++)
		{
			for(int j = 0; j < tiles.length; j++)
			{
				if(i != j && getHexVal(i) != -1 && getHexVal(i) == getHexVal(j))
				{
					JOptionPane.showMessageDialog(null, "Problem between tile "+tiles[i]+" and tile "+tiles[j], "Hexachecker", JOptionPane.INFORMATION_MESSAGE);
					bNoProblem = false;
				}
			}
		}
		if(bNoProblem)
			JOptionPane.showMessageDialog(null, "No problem detected =)", "Hexachecker", JOptionPane.INFORMATION_MESSAGE);
	}
}

